//
//  HMNewFeatureViewController.swift
//  HMWeibo1
//
//  Created by liGordon on 16/4/11.
//  Copyright © 2016年 lifuli. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class HMNewFeatureViewController: UICollectionViewController {
    
    /// 布局参数
    let layout = UICollectionViewFlowLayout()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    init() {
        super.init(collectionViewLayout: layout)
        
     
        // 设置layout 参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
    }

  
    

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(HMNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }




    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return NumberOfNewFeatureItem
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! HMNewFeatureCell
    
//        cell.backgroundColor = UIColor.orangeColor()
        
        cell.index = indexPath.item
    
        return cell
    }
}


// Mark -  扩展HMNewFeatureViewController 实现 scrollViewDidEndDecelerating 方法
extension HMNewFeatureViewController {
    // scrollView停止滚动时调用
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
       // HMPrint("scrollView停止滚动时调用")
        let index = Int((scrollView.contentOffset.x) / UIScreen.mainScreen().bounds.width)
        HMPrint("scrollView停止滚动时调用,页码: \(index)")
        // 判断是否是最后一页
        if index == NumberOfNewFeatureItem - 1 {
            let indexPath = NSIndexPath(forItem: index, inSection: 0)
            
            // 最后一页,让cell开始动画
            let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! HMNewFeatureCell
            cell.startAnimation()
        }
        
    }
}



/// 在一个文件里面写多个类
class HMNewFeatureCell: UICollectionViewCell {
    
    // 当前cell显示的页码
    var index: Int = 0 {
        didSet {
            // 当控制器给cell的index设置值后
            // 拼接图片名称
            let imageName = "new_feature_\(index + 1)"
            HMPrint("imageName: \(imageName)")
            bgImageView.image = UIImage(named: imageName)
            
            startButton.hidden = true
         
        }
    }
    
    // 重写 黑屏 不然不显示 系统回来调用这个方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func prepareUI() {
        // 1. 添加控件
        // 当时cell的时候 view都添加到contentView
        contentView.addSubview(bgImageView)
        contentView.addSubview(startButton)
        
        
        // 2. 添加约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        // 开始按钮
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView).offset(-160)
        }
    }
    
    
    
    // Mark: 动画方法
    func startAnimation() {
        // 显示按钮
        startButton.hidden = false
        
        // 将transform的缩放改0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startButton.transform = CGAffineTransformIdentity // 清空transform的缩放 , 1
            }) { (_) -> Void in
//             HMPrint("动画完成了")
        }
    }
    
    
    
    // Mark - 按钮点击事件
    @objc private func didClickStartButton() {
        // 切换控制器
        let controller = HMMainViewController()
        
       ( UIApplication.sharedApplication().delegate as! AppDelegate ).switchViewController(controller)
    }
    
    
    
    
    
    
    
    
    // Mark: 懒加载
    private lazy var bgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    
    /// 开始按钮
    private lazy var startButton: UIButton = {
        let button = UIButton()
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        
            button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
       button.setTitle("开始体验", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(HMNewFeatureLabelFontSize)
        
        button.sizeToFit()
        
        
        button.addTarget(self, action: "didClickStartButton", forControlEvents: .TouchUpInside)
        
        return button
        
    }()
    
}